(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.site.listController', listController);
	listController.$inject = [ '$scope', '$q', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService',
			'siteService', 'storageService'];
	function listController($scope, $q, Constants, gridService, modalService, layoutConfig, validatorConfig, authService, siteService, storageService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("siteList");
		//ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.init = function() {
			//siteService.initGeneral($scope, ctrl);
			var superBankFlag;
			siteService.loadSiteOptions().then(function (response){
				siteService.initSiteOptionData(ctrl, response);
				superBankFlag = ctrl.isSuperBank.data;
				
//				var siteCode = storageService.getUserInfo().siteCode;
				if ( !superBankFlag ) {
					var buttons = layoutConfig.buttonConfigs.buttonGroup.buttons;
					angular.forEach(buttons, function ( button, key) {
						if (button.name =='create'){
							buttons.splice(key, 1);
						}
					});
					ctrl.layoutConfig = layoutConfig;
				} else {
					ctrl.layoutConfig = layoutConfig;
				}
				
				ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
				ctrl.gridOptions.data = 'vm.searchCriteria.list';
				ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
				ctrl.search();
			});
			
		};
		ctrl.addCompany = function() {
			var filterCompanyCodes = [];
			ctrl.site.company.subCompanies = ctrl.site.company.subCompanies || [];
			for (var i = 0; i < ctrl.site.company.subCompanies.length; i++) {
				var item = ctrl.site.company.subCompanies[i];
				filterCompanyCodes.push(item.companyCode);
			}
			modalService.openCustomDialogs({
				   'title': 'Company Lookup',
				   'dialogsModule' : 'cda.company',
				   'params':{companyCode: ctrl.site.siteCode ,filterCompanyCodes: filterCompanyCodes },
				   'btnCallback' : {
						'ok' : function(selectedRows) {			
							if (!selectedRows || selectedRows.length == 0) {
								modalService.openErrorDialogs({
									messages : [ 'title.dialogs.info.norecsel' ]
								});
								return false;
							}							
							var subcompany = selectedRows[0];
							if (!ctrl.site.company.subCompanies) {								
								ctrl.site.company.subCompanies = [];
							}
							var exist = false;
							console.debug('selected subcompany is ' + subcompany.companyCode);
							for (var i = 0; i < ctrl.site.company.subCompanies.length; i++) {
								var item = ctrl.site.company.subCompanies[i];
								console.debug('compare to ' + item.companyCode);
								if (item.companyCode == subcompany.companyCode) {									
									exist = true;
									console.debug('company code has existed');
									break;
								}
							}
							if (!exist) {	
								console.debug('company code will be added');
								subcompany.childCompanyCode = subcompany.companyCode;
								subcompany.dataStatus = 'C';
								ctrl.site.company.subCompanies.push(subcompany);
							}															
						}
					}		
				   
				});			
		};
		ctrl.view = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.site.view', ctrl.selectedRows[0]);
		};

		ctrl.create = function() {
			$scope.go('cda.site.create');
		};

		ctrl.update = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.site.update', ctrl.selectedRows[0]);
		};

		ctrl.delete = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.site.delete', ctrl.selectedRows[0]);
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(
			function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			},angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			// now just support sort by one column
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);